#include <unistd.h>
#include <time.h>
#include "cnats.h"
#include "clog.h"
#include "threads.h"

class testWorker:public worker{
private:
    int threadNum;
public:
    testWorker(int num){
        threadNum = num;
    }
    void run(void){
        clog::d("worker:");
        clog::d(threadNum);
        clog::dl(" running");
        usleep(1000000);
        clog::d("END: ");
        clog::dl(threadNum);
    }
    void stop(){
        clog::dl("STOP Called");
    }
};

void testForThreads(){
    clog::dl("ThreadTestStart");
    const int numTestNum = 10;
    testWorker* workers[numTestNum];
    // srand((int)time(0));
    srand(0);
    threads ths(1);// = threads(1);
    ths.addJob(new testWorker(-1));
    for(int i = 0; i < numTestNum; i++){
        workers[i] = new testWorker(i);
        ths.addOneshot(workers[i]);
        clog::dl("enter add Jobs....");
        int x = 100;
        clog::dl(x);
        usleep(x);
        clog::dl("adding Jobs....");
    }
    clog::dl("ThreadTestEnd");
    ths.stop();
    for (int i = 0; i < numTestNum; ++i){
        delete workers[i];
    }
    clog::dl("Finished Thread Test");
}

class testHandler: public MsgHandler {
public:
    void execute(recvedMsgs *msg){
        clog::dl("Recved Msg");
    }
    void asyncErrorCallBack(){
        clog::dl("Err Call Back");
    }
    void asyncSuccCallBack(){
        clog::dl("Succ Call Back");
    }
};

int main(int argc, char const *argv[]){
    clog::setLevel(0);
    // srand
    clog::dl("Start");
    testForThreads();
    // cnats* conn = cnats::Connect("nats://localhost:4222,nats://localhost:4223,nats://localhost:4224");
    // conn->Subscribe("test",new testHandler());
    // conn->Publish("test1","asddd",5);
    // clog::dl("Enter Sleep");
    // while(1){
    //     sleep(100);
    // }
    return 0;
}
